﻿using System;
using System.Collections.Generic;
using Bbm.Persistence.Serializers;
using NUnit.Framework;

namespace Bbm.Persistence.Tests
{
    [TestFixture]
    public class SerializationTests
    {
        private static void TestRoundtrip<T>(T data)
        {
        	var serializer = new GenericJsonSerializer();

        	var serialized = serializer.Serialize(data);

            Assert.AreEqual(data, serializer.Deserialize<T>(serialized));
        }

        [Test]
        public void TestBasicSerialization()
        {
            TestRoundtrip(1);
            TestRoundtrip("1");
            TestRoundtrip(1.0);
            TestRoundtrip(DateTime.Today);
        }

        [Test]
        public void TestCollections()
        {
            var list = new List<string> { "123", "456" };

			var serializer = new GenericJsonSerializer();

			var serialized = serializer.Serialize(list);

            CollectionAssert.AreEqual(list, serializer.Deserialize<List<string>>(serialized));

            var dictionary = new Dictionary<string, int> {{"123", 123}, {"456", 456}};

			serialized = serializer.Serialize(dictionary);

			CollectionAssert.AreEqual(dictionary, serializer.Deserialize<Dictionary<string, int>>(serialized));
        }
    }
}
